package com.thymeleaf.demo.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.thymeleaf.demo.lib.AdminLib;
import com.thymeleaf.demo.model.User;
import com.thymeleaf.demo.mapper.UserMapper;
import com.thymeleaf.demo.myexception.MyExcption;
import com.thymeleaf.demo.service.UserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.thymeleaf.demo.util.result.ResultEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author sunny
 * @since 2020-12-22
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {

    @Autowired
    AdminLib adminLib;
    @Autowired
    UserMapper userMapper;

    @Override
    public List<Map> getUserList() {
        return userMapper.userList();
    }

    @Override
    public Boolean insertUser(User user) throws Exception {
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.eq("username", user.getUsername());
        // 判断用户名是否重复
        if(getOne(wrapper) != null){
            throw new MyExcption(ResultEnum.USERNAME_MORE.getCode());
        }
        //设置密码添加密码盐
        user = adminLib.setPwd(user.getPwd(), User.class, user);
        // 新增用户
        return save(user);
    }
}
